package org.flik.client.main;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.flik.client.main.event.TicketChangeEvent;
import org.flik.server.dto.IdName;
import org.flik.server.dto.InitialData;
import org.flik.server.dto.TicketSummary;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.BeforeSelectionEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SelectionChangeEvent;

public class MainScreen extends ResizeComposite {

	private static MainScreenUiBinder uiBinder = GWT.create(MainScreenUiBinder.class);

	interface MainScreenUiBinder extends UiBinder<Widget, MainScreen> {
	}
	@UiField
	TabLayoutPanel tabLayoutPanel;
	
	@UiField
	ListBox projects;
	
	@UiField
	Label welcomeMsg; 
	
	@UiField
	Button newTicketButton, editButton; 
	
	@UiField
	TicketSearchResultGrid ticketSearchResultGrid;
	private Map<Long, Widget> ticketTabs; 
	@UiHandler("newTicketButton")
	void onNewTicketButtonClick(ClickEvent event) {
		NewTicket.show(getFilterProjectId(), new TicketChangeEvent.Handler(){

			@Override
			public void onChange() {
				ticketSearchResultGrid.refreshData(getFilterProjectId());
			}});
	}
	@UiHandler("projects")
	void onProjectSelectionChange(ChangeEvent event) {
		ticketSearchResultGrid.refreshData(getFilterProjectId());
	}

	@UiHandler("tabLayoutPanel")
	void onTabLayoutPanelBeforeSelection(BeforeSelectionEvent<Integer> event){
		onResize();//this fixes the grid redraw issue when tab is changed. We could have used dataGrid.redraw() as well.
	}
	
	private Long getFilterProjectId(){
		return (projects == null || projects.getSelectedIndex() == -1?null:Long.parseLong(projects.getValue(projects.getSelectedIndex())));
	}
	private InitialData initialData;

	private void initProjectList(){
		List<IdName> projectList = initialData.getProjects();
		if (projectList != null)
			for (IdName project: projectList)
				projects.addItem(project.getName(), project.getId().toString());		
		projects.setSelectedIndex(initialData.getFocusedProjectIndex());
	}
	private void initWelcomeMessage(){
		welcomeMsg.setText("Welcome " + initialData.getLoggedInUserFullName());
	}
	public MainScreen(InitialData initialData) {
		this.initialData = initialData; 
		initWidget(uiBinder.createAndBindUi(this));
		initDataGridSelectionHandler();
		initWelcomeMessage();
		initProjectList();
		ticketSearchResultGrid.refreshData(getFilterProjectId());
	}
	
	@UiHandler("editButton")
	void onEditTicketClick(ClickEvent event){
		TicketSummary selectedTicket = ticketSearchResultGrid.getSelectedTicket();
		if (selectedTicket != null) {
			String title = selectedTicket.getTicketTitle();
			if (ticketTabs == null)
				ticketTabs = new HashMap<Long, Widget>();
			Widget ticketTabWidget = ticketTabs.get(selectedTicket.getTicketId());
			if (ticketTabWidget == null) {
				ticketTabWidget = new TicketDetailPanel(selectedTicket.getTicketId());
				tabLayoutPanel.add(ticketTabWidget, new HTML(title.length() > 25 ? title.substring(0, 25) + "..." : title));
				ticketTabs.put(selectedTicket.getTicketId(), ticketTabWidget);
			}
			tabLayoutPanel.selectTab(ticketTabWidget);
		}
	}
	private void initDataGridSelectionHandler() {
		ticketSearchResultGrid.addSelectionHandler(new SelectionChangeEvent.Handler() {
			
			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				editButton.setEnabled(ticketSearchResultGrid.getSelectedTicket() != null);
			}
		});
	}
}
